package com.fsoft.core.icon.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fsoft.core.annotation.SystemLog;
import com.fsoft.core.common.QueryParam;
import com.fsoft.core.common.base.BaseController;
import com.fsoft.core.icon.entity.SysIcon;
import com.fsoft.core.icon.service.IconService;
import com.fsoft.core.utils.DateTimeUtils;
import com.fsoft.core.utils.OgnlUtils;
import com.fsoft.core.utils.RetVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * F-Soft
 * @package com.fsoft.core.icon.controller
 * @author Fish
 * @email it.fish2010@foxmail.com
 * @date 2020-02-17
 * @CopyRight © F-Soft
 **/
@Controller
@RequestMapping("/icons")
public class IconController extends BaseController {

	@Autowired
	private IconService iconService;

	/**
	 * F-Soft 图标列表界面
	 * @author it_software(it.fish2010@foxmail.com)
	 * @date 2020-02-17
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public String list() throws Exception {
		return "/fsoft-web/icon/sysicons-list";
	}

	/**
	 * F-Soft 图标列表查询
	 * @author it_software(it.fish2010@foxmail.com)
	 * @date 2020-02-17
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/findList")
	public RetVo findList(@RequestParam Map<String, Object> params) throws Exception {
		QueryParam query = new QueryParam(params);
		Page<SysIcon> page = PageHelper.startPage(query.getPage(), query.getLimit());
		List<SysIcon> list = iconService.findList(query);
		return RetVo.ok(page.getTotal(), list);
	}

	/***
	 * F-Soft 跳转到字典编辑界面<br>
	 * @author Fish(it.fish2010@foxmail.com)
	 * @date 2019-11-05
	 * @param model
	 * @param rwid 字典的RWID标识
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/edit/{rwid}")
	public String edit(Model model, @PathVariable("rwid") String rwid) throws Exception {
		SysIcon icon = iconService.getEntity(rwid);
		model.addAttribute("model", icon);
		return "/fsoft-web/icon/sysicons-modify";
	}

	@RequestMapping("/add")
	public String add() throws Exception {
		return "/fsoft-web/icon/sysicons-modify";
	}

	/**
	 * F-Soft 添加图标
	 * @author it_software(it.fish2010@foxmail.com)
	 * @date 2020-02-17
	 * @param param
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@SystemLog("添加图标")
	@RequestMapping("/save")
	@RequiresPermissions("sys:icon:save")
	public RetVo saveItem(@RequestBody SysIcon param) throws Exception {
		try {
			if (StringUtils.isBlank(param.getId()))
				param.setId(OgnlUtils.randomUpperCaseId());
			param.setCreateTime(DateTimeUtils.getNowTime());
			param.setCreateUserId(getUserId());
			iconService.save(param);
			return RetVo.ok();
		} catch (Exception e) {
			return RetVo.error(e.getMessage());
		}
	}

	/**
	 * F-Soft 修改图标
	 * @author it_software(it.fish2010@foxmail.com)
	 * @date 2020-02-17
	 * @param param
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@SystemLog("修改图标")
	@RequestMapping("/modify")
	@RequiresPermissions("sys:icon:modify")
	public RetVo modifyItem(@RequestBody SysIcon param) throws Exception {
		try {
			param.setModifyUserId(getUserId());
			param.setModifyTime(DateTimeUtils.getNowTime());
			iconService.modify(param);
			return RetVo.ok();
		} catch (Exception e) {
			return RetVo.error(e.getMessage());
		}
	}

	/**
	 * F-Soft
	 * @author it_software(it.fish2010@foxmail.com)
	 * @date 2020-02-17
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@SystemLog("删除图标")
	@RequestMapping("/remove")
	@RequiresPermissions("sys:icon:remove")
	public RetVo removeItem(@RequestBody String[] ids) throws Exception {
		try {
			iconService.removeBatch(Arrays.asList(ids));
			return RetVo.ok(ids.length, "成功");
		} catch (Exception e) {
			return RetVo.error(e.getMessage());
		}
	}
}
